package org.hackystat.devcathlon.ui.page.matches.invitations;

import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.hackystat.devcathlon.engine.event.Event;
import org.hackystat.devcathlon.engine.match.Match;
import org.hackystat.devcathlon.engine.match.MatchManager;
import org.hackystat.devcathlon.engine.team.Team;
import org.hackystat.devcathlon.ui.DevcathlonSession;
import org.hackystat.devcathlon.ui.page.matches.MatchesSession;

/**
 * Panel that holds all the selected match invitation's details.
 * 
 * @author Ka Hung Phillip Lau
 *
 */
public class InvitationDetailPanel extends Panel {

  /** Support serialization. */
  private static final long serialVersionUID = 1L;

  /** Constructs the invitation detail panel with the wicket name, id. 
   * @param id The wicket id name.
   */
  public InvitationDetailPanel(String id) {
    super(id);
    MatchesSession session = DevcathlonSession.get().getMatchesSession();
    
    Match match = MatchManager.getInstance().getMatch(session.getSelectedMatch());
    
    String email = DevcathlonSession.get().getEmail();
    
    
    String startTime = "";
    String endTime = "";
    String matchName = "";
    List<Team> teams = new ArrayList<Team>();
    List<Event> matchEvents = new ArrayList<Event>();
    String inviteStatus = "";
    
    if (match != null) {
      try {
        startTime = match.printStartDateTime();
        endTime = match.printEndDateTime();
        matchName = match.getName();
        teams = new ArrayList<Team>(match.getTeams());
        matchEvents = new ArrayList<Event>(match.getEvents());
        for (Team team : match.getTeams()) {
          if (team.getOwner().getEmail().equals(email)) {
            inviteStatus = match.getInvitationStatus(team);
          }
        }
      }
      catch (Exception e) {
        e.printStackTrace();
      }
    }
    
    try {
      add(new Label("StartTime", startTime));
      add(new Label("EndTime", endTime));
      if (inviteStatus == null) {
        inviteStatus = "N/A";
      }
      add(new Label("InviteStatus", inviteStatus));
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    add(new Label("MatchName", matchName));
    
    ListView listView = new ListView("EventList", matchEvents) {
      private static final long serialVersionUID = 1L;

      @Override
      protected void populateItem(ListItem item) {
        Event eventName = (Event) item.getModelObject();
        item.add(new Label("EventName", eventName.getName()));
      }
    };
    add(listView);

    ListView listView2 = new ListView("TeamList", teams) {
      private static final long serialVersionUID = 1L;

      @Override
      protected void populateItem(ListItem item) {
        Team teamName = (Team) item.getModelObject();
        item.add(new Label("TeamName", teamName.getName()));
      }
    };
    add(listView2);
    
    
    // add accept button to form.
    Form form = new Form("InviteForm");
    
    Button accept = new Button("AcceptButton") {
      private static final long serialVersionUID = 1L;
      
      @Override
      public void onSubmit() {
        MatchesSession session = DevcathlonSession.get().getMatchesSession();
        
        Match match = MatchManager.getInstance().getMatch(session.getSelectedMatch());
        String email = DevcathlonSession.get().getEmail();
        if (match != null) {
          for (Team team : match.getTeams()) {
            if (team.getOwner().getEmail().equals(email)) {
              match.acceptMatchInvitation(team);
              // Update match instance (db).
              MatchManager.getInstance().updateMatch(match);
              session.setSelectedMatch("");
              setResponsePage(InvitationsPage.class);
            }
          }
        }
      }
    };
    
    // add cancel button to form.
    Button decline = new Button("DeclineButton") {
      private static final long serialVersionUID = 1L;
      
      @Override
      public void onSubmit() {
        MatchesSession session = DevcathlonSession.get().getMatchesSession();
        Match match = MatchManager.getInstance().getMatch(session.getSelectedMatch());
        String email = DevcathlonSession.get().getEmail();
        if (match != null ) {
          for (Team team : match.getTeams()) {
            if (team.getOwner().getEmail().equals(email)) {
              match.declineMatchInvitation(team);
              // Update match instance (db).
              MatchManager.getInstance().updateMatch(match);
              session.setSelectedMatch("");
              setResponsePage(InvitationsPage.class);
  
            }
          }
        }
      }
      
    };
    
    form.add(accept);
    form.add(decline);
    add(form);
    
  }

}
